package crawler;

import restclient.RESTClient;
import parser.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.InputStream;

/**
 *
 * @author Ilio Catallo, Eleonora Ciceri
 * @version: 1.0
 * 
 * Description: The class implements a generic web crawler that can
 *              retrieve content from a given web service starting from
 *              the query parameters and the list of relevant response 
 *              attributes
 * 
 * Date: Nov 16, 2011
 */
public abstract class Crawler {

    // Attributes
    protected RESTClient restClient;
    protected Parser parser;
    protected LinkedHashMap<String, String> query;
    protected Boolean isQueryToKeep;
    protected List<String> navigationPath;
    protected HashMap<String, String> attributes;
    
    /**
     * Constructor for the class Crawler
     * @param type
     * @param query
     * @param attributes
     * @param conditionAttribute 
     */
    public Crawler(String protocol, LinkedHashMap<String, String> query) {
        this.restClient = new RESTClient(protocol);
        this.query = query;
        this.attributes = new HashMap<String, String>();
        this.navigationPath = new ArrayList<String>();

    }
    
    /**
     * Method that performs the actual call to the web service
     * @return data
     */
    public List<Object> retrieve() {
        // Variables
        InputStream responseContent = null;
        List<Object> preProcData = new ArrayList<Object>();
        List<Object> procData = new ArrayList<Object>();   
        List<HashMap<String, String>> incrementAttributes = findIncrAttributes();
        
        this.parser.setAttributes(this.attributes);
        this.parser.setNavigationPath(this.navigationPath);
        
        for (int i = 0; i < incrementAttributes.size(); i++){
            
            // Create the query parameter setting the current page number
            HashMap<String, String> incrAttribute = incrementAttributes.get(i);
            this.query.putAll(incrAttribute);   
                  
            // Execute the query
            this.restClient.executeQuery(this.query);
            responseContent = this.restClient.getResponseContent();    

            // Parse the result
            this.parser.parse(responseContent);
            
            // Append the new pre-processed data
            preProcData.addAll(preProcessData(this.parser.getResponseAttributes()));
          
        }  
        
        // process the data
        procData =  processData(preProcData);
        
        // Close the connection and return the data
        this.restClient.closeConnection();
        return procData;
    }
    
    protected abstract List<HashMap<String, String>> findIncrAttributes();
    
    /**
     * Method that implements the business logic
     * @param responseAttributes 
     */
    protected abstract List<Object> preProcessData(List<HashMap<String, String>> responseAttributes);
    
    /**
     * Method that implements the business logic taking as an input the
     * response attributes from the web service
     * @param attributes
     * @return processed data
     */
    protected abstract ArrayList<Object> processData(List<Object> data);
    
    public static void main(String[] args) {
        // Variables
        LinkedHashMap<String, String> query = new LinkedHashMap<String, String>();
       
        // F\lickr stuff
        //query.put("URL", "http://api.flickr.com/services/rest/?");
        //query.put("method", "flickr.photos.search");
        //query.put("api_key", "a944e9305bc4519fcff20b4fc4ba92b3");
        //query.put("tags", "cat,sleep,dog");
        //query.put("tag_mode", "all");
        //query.put("sort","relevance");

        //FlickrPhotoCrawler fpCrawler = new FlickrPhotoCrawler(query,"/Users/ilio/Temporanea/Flickr");
        //fpCrawler.retrieve();
        
        // Google images stuff
        query.put("URL", "https://ajax.googleapis.com/ajax/services/search/images?");
        query.put("q", "coke%20logo");
        query.put("v", "1.0");
        query.put("key","ABQIAAAALuN3SqsakDUc87z-Fe6q-xTtbvPGGnSfpXmO1xymBh2iMITU5RSzbhf87zKqsgbH6ogOIQKzL3R-eg");
        
        GooglePhotoCrawler gpCrawler = new GooglePhotoCrawler(query, "/Users/ilio/Temporanea/Google");
        List<Object> retrieve = gpCrawler.retrieve();
 
        
    }
}
